package com.ocean.nc.service.impl;

import cn.hutool.core.io.resource.ResourceUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.github.pagehelper.util.StringUtil;
import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.common.core.page.ForeResult;
import com.ocean.common.core.text.Convert;
import com.ocean.common.utils.*;
import com.ocean.common.utils.file.FileUtils;
import com.ocean.common.utils.uuid.IdUtils;
import com.ocean.metadata.domain.OMetadata;
import com.ocean.metadata.service.IOMetadataService;
import com.ocean.nc.domain.ONcFile;
import com.ocean.nc.domain.ONcUnzip;
import com.ocean.nc.mapper.ONcFileMapper;
import com.ocean.nc.mapper.ONcUnzipMapper;
import com.ocean.nc.service.IONcFileService;
import com.ocean.nc.service.IONcUnzipService;
import com.ocean.previewElement.service.IOPreviewElementService;
import com.ocean.product.domain.OProduct;
import com.ocean.product.service.IOProductService;
import com.ocean.productCheck.domain.OProductCheckSms;
import com.ocean.productCheck.service.IOProductCheckSmsService;
import com.ocean.productRelease.domain.OProductRelease;
import com.ocean.productRelease.mapper.OProductReleaseMapper;
import com.ocean.productRelease.service.IOProductReleaseService;
import com.ocean.resources.domain.OResources;
import com.ocean.resources.service.IOResourcesService;
import com.ocean.system.domain.ODownLog;
import com.ocean.system.domain.ODownOrder;
import com.ocean.system.mapper.ODownLogMapper;
import com.ocean.system.mapper.ODownOrderMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.logging.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.math.BigDecimal;
import java.net.ConnectException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * nc文件解压
 *
 * @author ocean
 * @date 2023-02-13
 */
@Service
public class ONcUnzipServiceImpl implements IONcUnzipService {

    @Autowired
    private ONcUnzipMapper ncUnzipMapper;
    @Autowired
    private ODownOrderMapper downOrderMapper;
    @Autowired
    private ODownLogMapper downLogMapper;
    @Autowired
    private OProductReleaseMapper productReleaseMapper;

    @Override
    public int insertONcUnzip(ONcUnzip ncUnzip) {
        ncUnzip.setId(IdUtils.simpleUUID());
        return ncUnzipMapper.insertONcUnzip(ncUnzip);
    }

    @Override
    public List<ONcUnzip> getNcUnzipList(ONcUnzip ncUnzip) {
        return ncUnzipMapper.selectONcUnzipList(ncUnzip);
    }

    @Override
    @Transactional
    public void deleteNcUnzipByIds(String ids) {
        String[] idArr = Convert.toStrArray(ids);
        for (String id : idArr) {
            ONcUnzip ncUnzip = ncUnzipMapper.selectONcUnzipById(id);
            String filePath = ncUnzip.getFilePath()+"/"+ncUnzip.getFileName();
            List<ODownOrder> downOrderList = downOrderMapper.selectByResourcePath(filePath);
            if (CollectionUtils.isNotEmpty(downOrderList)) {
                ODownOrder downOrder = downOrderList.get(0);
                downOrder.setStatus("2");
                downOrderMapper.updateODownOrder(downOrder);
            }
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
                System.out.println("file delete: "+file.getName());
            }
            ncUnzip.setDel(1);
            ncUnzipMapper.updateONcUnzip(ncUnzip);
        }
    }

    @Override
    public int deleteCompeletly(String ids) {
        return ncUnzipMapper.deleteONcUnzipByIds(Convert.toStrArray(ids));
    }

    @Override
    public int updateONcUnzip(ONcUnzip ncUnzip) {
        return ncUnzipMapper.updateONcUnzip(ncUnzip);
    }

    @Override
    public List<ONcUnzip> selectONcFileByReleaseId(String releaseId) {
        return ncUnzipMapper.selectONcFileByReleaseId(releaseId);
    }

    @Override
    public List<ONcUnzip> getUnzip(String ncFileId) {
        ONcUnzip ncUnzip = new ONcUnzip();
        ncUnzip.setNcFileId(ncFileId);ncUnzip.setDel(0);
        List<ONcUnzip> list = ncUnzipMapper.selectONcUnzipList(ncUnzip);
        return list;
    }

    /**
     * 解压文件下载
     * @param unzipId
     * @return
     */
    @Override
    public String downloadUnzip(String unzipId) {
        // 下载记录加1
        ONcUnzip unzip = ncUnzipMapper.selectONcUnzipById(unzipId);
        OProductRelease pr = new OProductRelease();pr.setNcFileId(unzip.getNcFileId());
        List<OProductRelease> productReleases = productReleaseMapper.selectOProductReleaseList(pr);
        ODownLog downLog = new ODownLog();
        downLog.setDownTime(new Date());
        downLog.setProductId(productReleases.get(0).getProductId());
        SysUser sysUser = ShiroUtils.getSysUser();
        downLog.setUserId(sysUser.getUserId().toString());
        downLog.setId(IdUtils.simpleUUID());
        downLogMapper.insertODownLog(downLog);
        // path是指欲下载的文件的路径。
        String path = unzip.getFilePath() + "/" + unzip.getFileName();
        return path;
    }}
